home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_isinstance.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  10KB  |  285 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import unittest
  5. from test import test_support
  6. import sys
  7.  
  8. class TestIsInstanceExceptions(unittest.TestCase):
  9.     
  10.     def test_class_has_no_bases(self):
  11.         
  12.         class I(object):
  13.             
  14.             def getclass(self):
  15.                 pass
  16.  
  17.             __class__ = property(getclass)
  18.  
  19.         
  20.         class C(object):
  21.             
  22.             def getbases(self):
  23.                 return ()
  24.  
  25.             __bases__ = property(getbases)
  26.  
  27.         self.assertEqual(False, isinstance(I(), C()))
  28.  
  29.     
  30.     def test_bases_raises_other_than_attribute_error(self):
  31.         
  32.         class E(object):
  33.             
  34.             def getbases(self):
  35.                 raise RuntimeError
  36.  
  37.             __bases__ = property(getbases)
  38.  
  39.         
  40.         class I(object):
  41.             
  42.             def getclass(self):
  43.                 return E()
  44.  
  45.             __class__ = property(getclass)
  46.  
  47.         
  48.         class C(object):
  49.             
  50.             def getbases(self):
  51.                 return ()
  52.  
  53.             __bases__ = property(getbases)
  54.  
  55.         self.assertRaises(RuntimeError, isinstance, I(), C())
  56.  
  57.     
  58.     def test_dont_mask_non_attribute_error(self):
  59.         
  60.         class I:
  61.             pass
  62.  
  63.         
  64.         class C(object):
  65.             
  66.             def getbases(self):
  67.                 raise RuntimeError
  68.  
  69.             __bases__ = property(getbases)
  70.  
  71.         self.assertRaises(RuntimeError, isinstance, I(), C())
  72.  
  73.     
  74.     def test_mask_attribute_error(self):
  75.         
  76.         class I:
  77.             pass
  78.  
  79.         
  80.         class C(object):
  81.             
  82.             def getbases(self):
  83.                 raise AttributeError
  84.  
  85.             __bases__ = property(getbases)
  86.  
  87.         self.assertRaises(TypeError, isinstance, I(), C())
  88.  
  89.  
  90.  
  91. class TestIsSubclassExceptions(unittest.TestCase):
  92.     
  93.     def test_dont_mask_non_attribute_error(self):
  94.         
  95.         class C(object):
  96.             
  97.             def getbases(self):
  98.                 raise RuntimeError
  99.  
  100.             __bases__ = property(getbases)
  101.  
  102.         
  103.         class S(C):
  104.             pass
  105.  
  106.         self.assertRaises(RuntimeError, issubclass, C(), S())
  107.  
  108.     
  109.     def test_mask_attribute_error(self):
  110.         
  111.         class C(object):
  112.             
  113.             def getbases(self):
  114.                 raise AttributeError
  115.  
  116.             __bases__ = property(getbases)
  117.  
  118.         
  119.         class S(C):
  120.             pass
  121.  
  122.         self.assertRaises(TypeError, issubclass, C(), S())
  123.  
  124.     
  125.     def test_dont_mask_non_attribute_error_in_cls_arg(self):
  126.         
  127.         class B:
  128.             pass
  129.  
  130.         
  131.         class C(object):
  132.             
  133.             def getbases(self):
  134.                 raise RuntimeError
  135.  
  136.             __bases__ = property(getbases)
  137.  
  138.         self.assertRaises(RuntimeError, issubclass, B, C())
  139.  
  140.     
  141.     def test_mask_attribute_error_in_cls_arg(self):
  142.         
  143.         class B:
  144.             pass
  145.  
  146.         
  147.         class C(object):
  148.             
  149.             def getbases(self):
  150.                 raise AttributeError
  151.  
  152.             __bases__ = property(getbases)
  153.  
  154.         self.assertRaises(TypeError, issubclass, B, C())
  155.  
  156.  
  157.  
  158. class AbstractClass(object):
  159.     
  160.     def __init__(self, bases):
  161.         self.bases = bases
  162.  
  163.     
  164.     def getbases(self):
  165.         return self.bases
  166.  
  167.     __bases__ = property(getbases)
  168.     
  169.     def __call__(self):
  170.         return AbstractInstance(self)
  171.  
  172.  
  173.  
  174. class AbstractInstance(object):
  175.     
  176.     def __init__(self, klass):
  177.         self.klass = klass
  178.  
  179.     
  180.     def getclass(self):
  181.         return self.klass
  182.  
  183.     __class__ = property(getclass)
  184.  
  185. AbstractSuper = AbstractClass(bases = ())
  186. AbstractChild = AbstractClass(bases = (AbstractSuper,))
  187.  
  188. class Super:
  189.     pass
  190.  
  191.  
  192. class Child(Super):
  193.     pass
  194.  
  195.  
  196. class NewSuper(object):
  197.     pass
  198.  
  199.  
  200. class NewChild(NewSuper):
  201.     pass
  202.  
  203.  
  204. class TestIsInstanceIsSubclass(unittest.TestCase):
  205.     
  206.     def test_isinstance_normal(self):
  207.         self.assertEqual(True, isinstance(Super(), Super))
  208.         self.assertEqual(False, isinstance(Super(), Child))
  209.         self.assertEqual(False, isinstance(Super(), AbstractSuper))
  210.         self.assertEqual(False, isinstance(Super(), AbstractChild))
  211.         self.assertEqual(True, isinstance(Child(), Super))
  212.         self.assertEqual(False, isinstance(Child(), AbstractSuper))
  213.  
  214.     
  215.     def test_isinstance_abstract(self):
  216.         self.assertEqual(True, isinstance(AbstractSuper(), AbstractSuper))
  217.         self.assertEqual(False, isinstance(AbstractSuper(), AbstractChild))
  218.         self.assertEqual(False, isinstance(AbstractSuper(), Super))
  219.         self.assertEqual(False, isinstance(AbstractSuper(), Child))
  220.         self.assertEqual(True, isinstance(AbstractChild(), AbstractChild))
  221.         self.assertEqual(True, isinstance(AbstractChild(), AbstractSuper))
  222.         self.assertEqual(False, isinstance(AbstractChild(), Super))
  223.         self.assertEqual(False, isinstance(AbstractChild(), Child))
  224.  
  225.     
  226.     def test_subclass_normal(self):
  227.         self.assertEqual(True, issubclass(Super, Super))
  228.         self.assertEqual(False, issubclass(Super, AbstractSuper))
  229.         self.assertEqual(False, issubclass(Super, Child))
  230.         self.assertEqual(True, issubclass(Child, Child))
  231.         self.assertEqual(True, issubclass(Child, Super))
  232.         self.assertEqual(False, issubclass(Child, AbstractSuper))
  233.  
  234.     
  235.     def test_subclass_abstract(self):
  236.         self.assertEqual(True, issubclass(AbstractSuper, AbstractSuper))
  237.         self.assertEqual(False, issubclass(AbstractSuper, AbstractChild))
  238.         self.assertEqual(False, issubclass(AbstractSuper, Child))
  239.         self.assertEqual(True, issubclass(AbstractChild, AbstractChild))
  240.         self.assertEqual(True, issubclass(AbstractChild, AbstractSuper))
  241.         self.assertEqual(False, issubclass(AbstractChild, Super))
  242.         self.assertEqual(False, issubclass(AbstractChild, Child))
  243.  
  244.     
  245.     def test_subclass_tuple(self):
  246.         self.assertEqual(True, issubclass(Child, (Child,)))
  247.         self.assertEqual(True, issubclass(Child, (Super,)))
  248.         self.assertEqual(False, issubclass(Super, (Child,)))
  249.         self.assertEqual(True, issubclass(Super, (Child, Super)))
  250.         self.assertEqual(False, issubclass(Child, ()))
  251.         self.assertEqual(True, issubclass(Super, (Child, (Super,))))
  252.         self.assertEqual(True, issubclass(NewChild, (NewChild,)))
  253.         self.assertEqual(True, issubclass(NewChild, (NewSuper,)))
  254.         self.assertEqual(False, issubclass(NewSuper, (NewChild,)))
  255.         self.assertEqual(True, issubclass(NewSuper, (NewChild, NewSuper)))
  256.         self.assertEqual(False, issubclass(NewChild, ()))
  257.         self.assertEqual(True, issubclass(NewSuper, (NewChild, (NewSuper,))))
  258.         self.assertEqual(True, issubclass(int, (long, (float, int))))
  259.         self.assertEqual(True, issubclass(str, (unicode, (Child, NewChild, basestring))))
  260.  
  261.     
  262.     def test_subclass_recursion_limit(self):
  263.         self.assertRaises(RuntimeError, blowstack, issubclass, str, str)
  264.  
  265.     
  266.     def test_isinstance_recursion_limit(self):
  267.         self.assertRaises(RuntimeError, blowstack, isinstance, '', str)
  268.  
  269.  
  270.  
  271. def blowstack(fxn, arg, compare_to):
  272.     tuple_arg = (compare_to,)
  273.     for cnt in xrange(sys.getrecursionlimit() + 5):
  274.         tuple_arg = (tuple_arg,)
  275.         fxn(arg, tuple_arg)
  276.     
  277.  
  278.  
  279. def test_main():
  280.     test_support.run_unittest(TestIsInstanceExceptions, TestIsSubclassExceptions, TestIsInstanceIsSubclass)
  281.  
  282. if __name__ == '__main__':
  283.     test_main()
  284.  
  285.